{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 6.12 \u8bfb\u53d6\u5d4c\u5957\u548c\u53ef\u53d8\u957f\u4e8c\u8fdb\u5236\u6570\u636e\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u9700\u8981\u8bfb\u53d6\u5305\u542b\u5d4c\u5957\u6216\u8005\u53ef\u53d8\u957f\u8bb0\u5f55\u96c6\u5408\u7684\u590d\u6742\u4e8c\u8fdb\u5236\u683c\u5f0f\u7684\u6570\u636e\u3002\u8fd9\u4e9b\u6570\u636e\u53ef\u80fd\u5305\u542b\u56fe\u7247\u3001\u89c6\u9891\u3001\u7535\u5b50\u5730\u56fe\u6587\u4ef6\u7b49\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "struct \u6a21\u5757\u53ef\u88ab\u7528\u6765\u7f16\u7801/\u89e3\u7801\u51e0\u4e4e\u6240\u6709\u7c7b\u578b\u7684\u4e8c\u8fdb\u5236\u7684\u6570\u636e\u7ed3\u6784\u3002\u4e3a\u4e86\u89e3\u91ca\u6e05\u695a\u8fd9\u79cd\u6570\u636e\uff0c\u5047\u8bbe\u4f60\u7528\u4e0b\u9762\u7684Python\u6570\u636e\u7ed3\u6784\n\u6765\u8868\u793a\u4e00\u4e2a\u7ec4\u6210\u4e00\u7cfb\u5217\u591a\u8fb9\u5f62\u7684\u70b9\u7684\u96c6\u5408\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "polys = [\n    [ (1.0, 2.5), (3.5, 4.0), (2.5, 1.5) ],\n    [ (7.0, 1.2), (5.1, 3.0), (0.5, 7.5), (0.8, 9.0) ],\n    [ (3.4, 6.3), (1.2, 0.5), (4.6, 9.2) ],\n]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u73b0\u5728\u5047\u8bbe\u8fd9\u4e2a\u6570\u636e\u88ab\u7f16\u7801\u5230\u4e00\u4e2a\u4ee5\u4e0b\u5217\u5934\u90e8\u5f00\u59cb\u7684\u4e8c\u8fdb\u5236\u6587\u4ef6\u4e2d\u53bb\u4e86\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "+------+--------+------------------------------------+\n|Byte  | Type   |  Description                       |\n+======+========+====================================+\n|0     | int    |  \u6587\u4ef6\u4ee3\u7801\uff080x1234\uff0c\u5c0f\u7aef\uff09          |\n+------+--------+------------------------------------+\n|4     | double |  x \u7684\u6700\u5c0f\u503c\uff08\u5c0f\u7aef\uff09                |\n+------+--------+------------------------------------+\n|12    | double |  y \u7684\u6700\u5c0f\u503c\uff08\u5c0f\u7aef\uff09                |\n+------+--------+------------------------------------+\n|20    | double |  x \u7684\u6700\u5927\u503c\uff08\u5c0f\u7aef\uff09                |\n+------+--------+------------------------------------+\n|28    | double |  y \u7684\u6700\u5927\u503c\uff08\u5c0f\u7aef\uff09                |\n+------+--------+------------------------------------+\n|36    | int    |  \u4e09\u89d2\u5f62\u6570\u91cf\uff08\u5c0f\u7aef\uff09                |\n+------+--------+------------------------------------+"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7d27\u8ddf\u7740\u5934\u90e8\u662f\u4e00\u7cfb\u5217\u7684\u591a\u8fb9\u5f62\u8bb0\u5f55\uff0c\u7f16\u7801\u683c\u5f0f\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "+------+--------+-------------------------------------------+\n|Byte  | Type   |  Description                              |\n+======+========+===========================================+\n|0     | int    |  \u8bb0\u5f55\u957f\u5ea6\uff08N\u5b57\u8282\uff09                        |\n+------+--------+-------------------------------------------+\n|4-N   | Points |  (X,Y) \u5750\u6807\uff0c\u4ee5\u6d6e\u70b9\u6570\u8868\u793a                 |\n+------+--------+-------------------------------------------+"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u5199\u8fd9\u6837\u7684\u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5982\u4e0b\u7684Python\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import struct\nimport itertools\n\ndef write_polys(filename, polys):\n    # Determine bounding box\n    flattened = list(itertools.chain(*polys))\n    min_x = min(x for x, y in flattened)\n    max_x = max(x for x, y in flattened)\n    min_y = min(y for x, y in flattened)\n    max_y = max(y for x, y in flattened)\n    with open(filename, 'wb') as f:\n        f.write(struct.pack('<iddddi', 0x1234,\n                            min_x, min_y,\n                            max_x, max_y,\n                            len(polys)))\n        for poly in polys:\n            size = len(poly) * struct.calcsize('<dd')\n            f.write(struct.pack('<i', size + 4))\n            for pt in poly:\n                f.write(struct.pack('<dd', *pt))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c06\u6570\u636e\u8bfb\u53d6\u56de\u6765\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u5229\u7528\u51fd\u6570 struct.unpack() \uff0c\u4ee3\u7801\u5f88\u76f8\u4f3c\uff0c\u57fa\u672c\u5c31\u662f\u4e0a\u9762\u5199\u64cd\u4f5c\u7684\u9006\u5e8f\u3002\u5982\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def read_polys(filename):\n    with open(filename, 'rb') as f:\n        # Read the header\n        header = f.read(40)\n        file_code, min_x, min_y, max_x, max_y, num_polys = \\\n            struct.unpack('<iddddi', header)\n        polys = []\n        for n in range(num_polys):\n            pbytes, = struct.unpack('<i', f.read(4))\n            poly = []\n            for m in range(pbytes // 16):\n                pt = struct.unpack('<dd', f.read(16))\n                poly.append(pt)\n            polys.append(poly)\n    return polys"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c3d\u7ba1\u8fd9\u4e2a\u4ee3\u7801\u53ef\u4ee5\u5de5\u4f5c\uff0c\u4f46\u662f\u91cc\u9762\u6df7\u6742\u4e86\u5f88\u591a\u8bfb\u53d6\u3001\u89e3\u5305\u6570\u636e\u7ed3\u6784\u548c\u5176\u4ed6\u7ec6\u8282\u7684\u4ee3\u7801\u3002\u5982\u679c\u7528\u8fd9\u6837\u7684\u4ee3\u7801\u6765\u5904\u7406\u771f\u5b9e\u7684\u6570\u636e\u6587\u4ef6\uff0c\n\u90a3\u672a\u514d\u4e5f\u592a\u7e41\u6742\u4e86\u70b9\u3002\u56e0\u6b64\u5f88\u663e\u7136\u5e94\u8be5\u6709\u53e6\u4e00\u79cd\u89e3\u51b3\u65b9\u6cd5\u53ef\u4ee5\u7b80\u5316\u8fd9\u4e9b\u6b65\u9aa4\uff0c\u8ba9\u7a0b\u5e8f\u5458\u53ea\u5173\u6ce8\u81ea\u6700\u91cd\u8981\u7684\u4e8b\u60c5\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u672c\u5c0f\u8282\u63a5\u4e0b\u6765\u7684\u90e8\u5206\uff0c\u6211\u4f1a\u9010\u6b65\u6f14\u793a\u4e00\u4e2a\u66f4\u52a0\u4f18\u79c0\u7684\u89e3\u6790\u5b57\u8282\u6570\u636e\u7684\u65b9\u6848\u3002\n\u76ee\u6807\u662f\u53ef\u4ee5\u7ed9\u7a0b\u5e8f\u5458\u63d0\u4f9b\u4e00\u4e2a\u9ad8\u7ea7\u7684\u6587\u4ef6\u683c\u5f0f\u5316\u65b9\u6cd5\uff0c\u5e76\u7b80\u5316\u8bfb\u53d6\u548c\u89e3\u5305\u6570\u636e\u7684\u7ec6\u8282\u3002\u4f46\u662f\u6211\u8981\u5148\u63d0\u9192\u4f60\uff0c\n\u672c\u5c0f\u8282\u63a5\u4e0b\u6765\u7684\u90e8\u5206\u4ee3\u7801\u5e94\u8be5\u662f\u6574\u672c\u4e66\u4e2d\u6700\u590d\u6742\u6700\u9ad8\u7ea7\u7684\u4f8b\u5b50\uff0c\u4f7f\u7528\u4e86\u5927\u91cf\u7684\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u548c\u5143\u7f16\u7a0b\u6280\u672f\u3002\n\u4e00\u5b9a\u8981\u4ed4\u7ec6\u7684\u9605\u8bfb\u6211\u4eec\u7684\u8ba8\u8bba\u90e8\u5206\uff0c\u53e6\u5916\u4e5f\u8981\u53c2\u8003\u4e0b\u5176\u4ed6\u7ae0\u8282\u5185\u5bb9\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9996\u5148\uff0c\u5f53\u8bfb\u53d6\u5b57\u8282\u6570\u636e\u7684\u65f6\u5019\uff0c\u901a\u5e38\u5728\u6587\u4ef6\u5f00\u59cb\u90e8\u5206\u4f1a\u5305\u542b\u6587\u4ef6\u5934\u548c\u5176\u4ed6\u7684\u6570\u636e\u7ed3\u6784\u3002\n\u5c3d\u7ba1struct\u6a21\u5757\u53ef\u4ee5\u89e3\u5305\u8fd9\u4e9b\u6570\u636e\u5230\u4e00\u4e2a\u5143\u7ec4\u4e2d\u53bb\uff0c\u53e6\u5916\u4e00\u79cd\u8868\u793a\u8fd9\u79cd\u4fe1\u606f\u7684\u65b9\u5f0f\u5c31\u662f\u4f7f\u7528\u4e00\u4e2a\u7c7b\u3002\n\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import struct\n\nclass StructField:\n    '''\n    Descriptor representing a simple structure field\n    '''\n    def __init__(self, format, offset):\n        self.format = format\n        self.offset = offset\n    def __get__(self, instance, cls):\n        if instance is None:\n            return self\n        else:\n            r = struct.unpack_from(self.format, instance._buffer, self.offset)\n            return r[0] if len(r) == 1 else r\n\nclass Structure:\n    def __init__(self, bytedata):\n        self._buffer = memoryview(bytedata)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u91cc\u6211\u4eec\u4f7f\u7528\u4e86\u4e00\u4e2a\u63cf\u8ff0\u5668\u6765\u8868\u793a\u6bcf\u4e2a\u7ed3\u6784\u5b57\u6bb5\uff0c\u6bcf\u4e2a\u63cf\u8ff0\u5668\u5305\u542b\u4e00\u4e2a\u7ed3\u6784\u517c\u5bb9\u683c\u5f0f\u7684\u4ee3\u7801\u4ee5\u53ca\u4e00\u4e2a\u5b57\u8282\u504f\u79fb\u91cf\uff0c\n\u5b58\u50a8\u5728\u5185\u90e8\u7684\u5185\u5b58\u7f13\u51b2\u4e2d\u3002\u5728 __get__() \u65b9\u6cd5\u4e2d\uff0cstruct.unpack_from()\n\u51fd\u6570\u88ab\u7528\u6765\u4ece\u7f13\u51b2\u4e2d\u89e3\u5305\u4e00\u4e2a\u503c\uff0c\u7701\u53bb\u4e86\u989d\u5916\u7684\u5206\u7247\u6216\u590d\u5236\u64cd\u4f5c\u6b65\u9aa4\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Structure \u7c7b\u5c31\u662f\u4e00\u4e2a\u57fa\u7840\u7c7b\uff0c\u63a5\u53d7\u5b57\u8282\u6570\u636e\u5e76\u5b58\u50a8\u5728\u5185\u90e8\u7684\u5185\u5b58\u7f13\u51b2\u4e2d\uff0c\u5e76\u88ab StructField \u63cf\u8ff0\u5668\u4f7f\u7528\u3002\n\u8fd9\u91cc\u4f7f\u7528\u4e86 memoryview() \uff0c\u6211\u4eec\u4f1a\u5728\u540e\u9762\u8be6\u7ec6\u8bb2\u89e3\u5b83\u662f\u7528\u6765\u5e72\u561b\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u8fd9\u4e2a\u4ee3\u7801\uff0c\u4f60\u73b0\u5728\u5c31\u80fd\u5b9a\u4e49\u4e00\u4e2a\u9ad8\u5c42\u6b21\u7684\u7ed3\u6784\u5bf9\u8c61\u6765\u8868\u793a\u4e0a\u9762\u8868\u683c\u4fe1\u606f\u6240\u671f\u671b\u7684\u6587\u4ef6\u683c\u5f0f\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class PolyHeader(Structure):\n    file_code = StructField('<i', 0)\n    min_x = StructField('<d', 4)\n    min_y = StructField('<d', 12)\n    max_x = StructField('<d', 20)\n    max_y = StructField('<d', 28)\n    num_polys = StructField('<i', 36)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0b\u9762\u7684\u4f8b\u5b50\u5229\u7528\u8fd9\u4e2a\u7c7b\u6765\u8bfb\u53d6\u4e4b\u524d\u6211\u4eec\u5199\u5165\u7684\u591a\u8fb9\u5f62\u6570\u636e\u7684\u5934\u90e8\u6570\u636e\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = open('polys.bin', 'rb')\nphead = PolyHeader(f.read(40))\nphead.file_code == 0x1234"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.min_x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.min_y"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.max_x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.max_y"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.num_polys"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u5f88\u6709\u8da3\uff0c\u4e0d\u8fc7\u8fd9\u79cd\u65b9\u5f0f\u8fd8\u662f\u6709\u4e00\u4e9b\u70e6\u4eba\u7684\u5730\u65b9\u3002\u9996\u5148\uff0c\u5c3d\u7ba1\u4f60\u83b7\u5f97\u4e86\u4e00\u4e2a\u7c7b\u63a5\u53e3\u7684\u4fbf\u5229\uff0c\n\u4f46\u662f\u8fd9\u4e2a\u4ee3\u7801\u8fd8\u662f\u6709\u70b9\u81c3\u80bf\uff0c\u8fd8\u9700\u8981\u4f7f\u7528\u8005\u6307\u5b9a\u5f88\u591a\u5e95\u5c42\u7684\u7ec6\u8282(\u6bd4\u5982\u91cd\u590d\u4f7f\u7528 StructField \uff0c\u6307\u5b9a\u504f\u79fb\u91cf\u7b49)\u3002\n\u53e6\u5916\uff0c\u8fd4\u56de\u7684\u7ed3\u679c\u7c7b\u540c\u6837\u786e\u5b9e\u4e00\u4e9b\u4fbf\u5229\u7684\u65b9\u6cd5\u6765\u8ba1\u7b97\u7ed3\u6784\u7684\u603b\u6570\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4efb\u4f55\u65f6\u5019\u53ea\u8981\u4f60\u9047\u5230\u4e86\u50cf\u8fd9\u6837\u5197\u4f59\u7684\u7c7b\u5b9a\u4e49\uff0c\u4f60\u5e94\u8be5\u8003\u8651\u4e0b\u4f7f\u7528\u7c7b\u88c5\u9970\u5668\u6216\u5143\u7c7b\u3002\n\u5143\u7c7b\u6709\u4e00\u4e2a\u7279\u6027\u5c31\u662f\u5b83\u80fd\u591f\u88ab\u7528\u6765\u586b\u5145\u8bb8\u591a\u4f4e\u5c42\u7684\u5b9e\u73b0\u7ec6\u8282\uff0c\u4ece\u800c\u91ca\u653e\u4f7f\u7528\u8005\u7684\u8d1f\u62c5\u3002\n\u4e0b\u9762\u6211\u6765\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u4f7f\u7528\u5143\u7c7b\u7a0d\u5fae\u6539\u9020\u4e0b\u6211\u4eec\u7684 Structure \u7c7b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class StructureMeta(type):\n    '''\n    Metaclass that automatically creates StructField descriptors\n    '''\n    def __init__(self, clsname, bases, clsdict):\n        fields = getattr(self, '_fields_', [])\n        byte_order = ''\n        offset = 0\n        for format, fieldname in fields:\n            if format.startswith(('<','>','!','@')):\n                byte_order = format[0]\n                format = format[1:]\n            format = byte_order + format\n            setattr(self, fieldname, StructField(format, offset))\n            offset += struct.calcsize(format)\n        setattr(self, 'struct_size', offset)\n\nclass Structure(metaclass=StructureMeta):\n    def __init__(self, bytedata):\n        self._buffer = bytedata\n\n    @classmethod\n    def from_file(cls, f):\n        return cls(f.read(cls.struct_size))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u65b0\u7684 Structure \u7c7b\uff0c\u4f60\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u5b9a\u4e49\u4e00\u4e2a\u7ed3\u6784\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class PolyHeader(Structure):\n    _fields_ = [\n        ('<i', 'file_code'),\n        ('d', 'min_x'),\n        ('d', 'min_y'),\n        ('d', 'max_x'),\n        ('d', 'max_y'),\n        ('i', 'num_polys')\n    ]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6b63\u5982\u4f60\u6240\u89c1\uff0c\u8fd9\u6837\u5199\u5c31\u7b80\u5355\u591a\u4e86\u3002\u6211\u4eec\u6dfb\u52a0\u7684\u7c7b\u65b9\u6cd5 from_file()\n\u8ba9\u6211\u4eec\u5728\u4e0d\u9700\u8981\u77e5\u9053\u4efb\u4f55\u6570\u636e\u7684\u5927\u5c0f\u548c\u7ed3\u6784\u7684\u60c5\u51b5\u4e0b\u5c31\u80fd\u8f7b\u677e\u7684\u4ece\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\u3002\u6bd4\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = open('polys.bin', 'rb')\nphead = PolyHeader.from_file(f)\nphead.file_code == 0x1234"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.min_x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.min_y"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.max_x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.max_y"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.num_polys"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u65e6\u4f60\u5f00\u59cb\u4f7f\u7528\u4e86\u5143\u7c7b\uff0c\u4f60\u5c31\u53ef\u4ee5\u8ba9\u5b83\u53d8\u5f97\u66f4\u52a0\u667a\u80fd\u3002\u4f8b\u5982\uff0c\u5047\u8bbe\u4f60\u8fd8\u60f3\u652f\u6301\u5d4c\u5957\u7684\u5b57\u8282\u7ed3\u6784\uff0c\n\u4e0b\u9762\u662f\u5bf9\u524d\u9762\u5143\u7c7b\u7684\u4e00\u4e2a\u5c0f\u7684\u6539\u8fdb\uff0c\u63d0\u4f9b\u4e86\u4e00\u4e2a\u65b0\u7684\u8f85\u52a9\u63cf\u8ff0\u5668\u6765\u8fbe\u5230\u60f3\u8981\u7684\u6548\u679c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class NestedStruct:\n    '''\n    Descriptor representing a nested structure\n    '''\n    def __init__(self, name, struct_type, offset):\n        self.name = name\n        self.struct_type = struct_type\n        self.offset = offset\n\n    def __get__(self, instance, cls):\n        if instance is None:\n            return self\n        else:\n            data = instance._buffer[self.offset:\n                            self.offset+self.struct_type.struct_size]\n            result = self.struct_type(data)\n            # Save resulting structure back on instance to avoid\n            # further recomputation of this step\n            setattr(instance, self.name, result)\n            return result\n\nclass StructureMeta(type):\n    '''\n    Metaclass that automatically creates StructField descriptors\n    '''\n    def __init__(self, clsname, bases, clsdict):\n        fields = getattr(self, '_fields_', [])\n        byte_order = ''\n        offset = 0\n        for format, fieldname in fields:\n            if isinstance(format, StructureMeta):\n                setattr(self, fieldname,\n                        NestedStruct(fieldname, format, offset))\n                offset += format.struct_size\n            else:\n                if format.startswith(('<','>','!','@')):\n                    byte_order = format[0]\n                    format = format[1:]\n                format = byte_order + format\n                setattr(self, fieldname, StructField(format, offset))\n                offset += struct.calcsize(format)\n        setattr(self, 'struct_size', offset)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u6bb5\u4ee3\u7801\u4e2d\uff0cNestedStruct \u63cf\u8ff0\u5668\u88ab\u7528\u6765\u53e0\u52a0\u53e6\u5916\u4e00\u4e2a\u5b9a\u4e49\u5728\u67d0\u4e2a\u5185\u5b58\u533a\u57df\u4e0a\u7684\u7ed3\u6784\u3002\n\u5b83\u901a\u8fc7\u5c06\u539f\u59cb\u5185\u5b58\u7f13\u51b2\u8fdb\u884c\u5207\u7247\u64cd\u4f5c\u540e\u5b9e\u4f8b\u5316\u7ed9\u5b9a\u7684\u7ed3\u6784\u7c7b\u578b\u3002\u7531\u4e8e\u5e95\u5c42\u7684\u5185\u5b58\u7f13\u51b2\u533a\u662f\u901a\u8fc7\u4e00\u4e2a\u5185\u5b58\u89c6\u56fe\u521d\u59cb\u5316\u7684\uff0c\n\u6240\u4ee5\u8fd9\u79cd\u5207\u7247\u64cd\u4f5c\u4e0d\u4f1a\u5f15\u53d1\u4efb\u4f55\u7684\u989d\u5916\u7684\u5185\u5b58\u590d\u5236\u3002\u76f8\u53cd\uff0c\u5b83\u4ec5\u4ec5\u5c31\u662f\u4e4b\u524d\u7684\u5185\u5b58\u7684\u4e00\u4e2a\u53e0\u52a0\u800c\u5df2\u3002\n\u53e6\u5916\uff0c\u4e3a\u4e86\u9632\u6b62\u91cd\u590d\u5b9e\u4f8b\u5316\uff0c\u901a\u8fc7\u4f7f\u7528\u548c8.10\u5c0f\u8282\u540c\u6837\u7684\u6280\u672f\uff0c\u63cf\u8ff0\u5668\u4fdd\u5b58\u4e86\u8be5\u5b9e\u4f8b\u4e2d\u7684\u5185\u90e8\u7ed3\u6784\u5bf9\u8c61\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u8fd9\u4e2a\u65b0\u7684\u4fee\u6b63\u7248\uff0c\u4f60\u5c31\u53ef\u4ee5\u50cf\u4e0b\u9762\u8fd9\u6837\u7f16\u5199\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class Point(Structure):\n    _fields_ = [\n        ('<d', 'x'),\n        ('d', 'y')\n    ]\n\nclass PolyHeader(Structure):\n    _fields_ = [\n        ('<i', 'file_code'),\n        (Point, 'min'), # nested struct\n        (Point, 'max'), # nested struct\n        ('i', 'num_polys')\n    ]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4ee4\u4eba\u60ca\u8bb6\u7684\u662f\uff0c\u5b83\u4e5f\u80fd\u6309\u7167\u9884\u671f\u7684\u6b63\u5e38\u5de5\u4f5c\uff0c\u6211\u4eec\u5b9e\u9645\u64cd\u4f5c\u4e0b\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = open('polys.bin', 'rb')\nphead = PolyHeader.from_file(f)\nphead.file_code == 0x1234"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.min # Nested structure"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.min.x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.min.y"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.max.x"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.max.y"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "phead.num_polys"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5230\u76ee\u524d\u4e3a\u6b62\uff0c\u4e00\u4e2a\u5904\u7406\u5b9a\u957f\u8bb0\u5f55\u7684\u6846\u67b6\u5df2\u7ecf\u5199\u597d\u4e86\u3002\u4f46\u662f\u5982\u679c\u7ec4\u4ef6\u8bb0\u5f55\u662f\u53d8\u957f\u7684\u5462\uff1f\n\u6bd4\u5982\uff0c\u591a\u8fb9\u5f62\u6587\u4ef6\u5305\u542b\u53d8\u957f\u7684\u90e8\u5206\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e00\u79cd\u65b9\u6848\u662f\u5199\u4e00\u4e2a\u7c7b\u6765\u8868\u793a\u5b57\u8282\u6570\u636e\uff0c\u540c\u65f6\u5199\u4e00\u4e2a\u5de5\u5177\u51fd\u6570\u6765\u901a\u8fc7\u591a\u5c11\u65b9\u5f0f\u89e3\u6790\u5185\u5bb9\u3002\u8ddf6.11\u5c0f\u8282\u7684\u4ee3\u7801\u5f88\u7c7b\u4f3c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class SizedRecord:\n    def __init__(self, bytedata):\n        self._buffer = memoryview(bytedata)\n\n    @classmethod\n    def from_file(cls, f, size_fmt, includes_size=True):\n        sz_nbytes = struct.calcsize(size_fmt)\n        sz_bytes = f.read(sz_nbytes)\n        sz, = struct.unpack(size_fmt, sz_bytes)\n        buf = f.read(sz - includes_size * sz_nbytes)\n        return cls(buf)\n\n    def iter_as(self, code):\n        if isinstance(code, str):\n            s = struct.Struct(code)\n            for off in range(0, len(self._buffer), s.size):\n                yield s.unpack_from(self._buffer, off)\n        elif isinstance(code, StructureMeta):\n            size = code.struct_size\n            for off in range(0, len(self._buffer), size):\n                data = self._buffer[off:off+size]\n                yield code(data)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7c7b\u65b9\u6cd5 SizedRecord.from_file() \u662f\u4e00\u4e2a\u5de5\u5177\uff0c\u7528\u6765\u4ece\u4e00\u4e2a\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5e26\u5927\u5c0f\u524d\u7f00\u7684\u6570\u636e\u5757\uff0c\n\u8fd9\u4e5f\u662f\u5f88\u591a\u6587\u4ef6\u683c\u5f0f\u5e38\u7528\u7684\u65b9\u5f0f\u3002\u4f5c\u4e3a\u8f93\u5165\uff0c\u5b83\u63a5\u53d7\u4e00\u4e2a\u5305\u542b\u5927\u5c0f\u7f16\u7801\u7684\u7ed3\u6784\u683c\u5f0f\u7f16\u7801\uff0c\u5e76\u4e14\u4e5f\u662f\u81ea\u5df1\u5f62\u5f0f\u3002\n\u53ef\u9009\u7684 includes_size \u53c2\u6570\u6307\u5b9a\u4e86\u5b57\u8282\u6570\u662f\u5426\u5305\u542b\u5934\u90e8\u5927\u5c0f\u3002\n\u4e0b\u9762\u662f\u4e00\u4e2a\u4f8b\u5b50\u6559\u4f60\u600e\u6837\u4f7f\u7528\u4ece\u591a\u8fb9\u5f62\u6587\u4ef6\u4e2d\u8bfb\u53d6\u5355\u72ec\u7684\u591a\u8fb9\u5f62\u6570\u636e\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "f = open('polys.bin', 'rb')\nphead = PolyHeader.from_file(f)\nphead.num_polys"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "polydata = [ SizedRecord.from_file(f, '<i')\n            for n in range(phead.num_polys) ]\npolydata"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u53ef\u4ee5\u770b\u51fa\uff0cSizedRecord \u5b9e\u4f8b\u7684\u5185\u5bb9\u8fd8\u6ca1\u6709\u88ab\u89e3\u6790\u51fa\u6765\u3002\n\u53ef\u4ee5\u4f7f\u7528 iter_as() \u65b9\u6cd5\u6765\u8fbe\u5230\u76ee\u7684\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u63a5\u53d7\u4e00\u4e2a\u7ed3\u6784\u683c\u5f0f\u5316\u7f16\u7801\u6216\u8005\u662f Structure \u7c7b\u4f5c\u4e3a\u8f93\u5165\u3002\n\u8fd9\u6837\u5b50\u53ef\u4ee5\u5f88\u7075\u6d3b\u7684\u53bb\u89e3\u6790\u6570\u636e\uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "for n, poly in enumerate(polydata):\n    print('Polygon', n)\n    for p in poly.iter_as('<dd'):\n        print(p)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "for n, poly in enumerate(polydata):\n    print('Polygon', n)\n    for p in poly.iter_as(Point):\n        print(p.x, p.y)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5c06\u6240\u6709\u8fd9\u4e9b\u7ed3\u5408\u8d77\u6765\uff0c\u4e0b\u9762\u662f\u4e00\u4e2a read_polys() \u51fd\u6570\u7684\u53e6\u5916\u4e00\u4e2a\u4fee\u6b63\u7248\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class Point(Structure):\n    _fields_ = [\n        ('<d', 'x'),\n        ('d', 'y')\n    ]\n\nclass PolyHeader(Structure):\n    _fields_ = [\n        ('<i', 'file_code'),\n        (Point, 'min'),\n        (Point, 'max'),\n        ('i', 'num_polys')\n    ]\n\ndef read_polys(filename):\n    polys = []\n    with open(filename, 'rb') as f:\n        phead = PolyHeader.from_file(f)\n        for n in range(phead.num_polys):\n            rec = SizedRecord.from_file(f, '<i')\n            poly = [ (p.x, p.y) for p in rec.iter_as(Point) ]\n            polys.append(poly)\n    return polys"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e00\u8282\u5411\u4f60\u5c55\u793a\u4e86\u8bb8\u591a\u9ad8\u7ea7\u7684\u7f16\u7a0b\u6280\u672f\uff0c\u5305\u62ec\u63cf\u8ff0\u5668\uff0c\u5ef6\u8fdf\u8ba1\u7b97\uff0c\u5143\u7c7b\uff0c\u7c7b\u53d8\u91cf\u548c\u5185\u5b58\u89c6\u56fe\u3002\n\u7136\u800c\uff0c\u5b83\u4eec\u90fd\u4e3a\u4e86\u540c\u4e00\u4e2a\u7279\u5b9a\u7684\u76ee\u6807\u670d\u52a1\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0a\u9762\u7684\u5b9e\u73b0\u7684\u4e00\u4e2a\u4e3b\u8981\u7279\u5f81\u662f\u5b83\u662f\u57fa\u4e8e\u61d2\u89e3\u5305\u7684\u601d\u60f3\u3002\u5f53\u4e00\u4e2a Structure \u5b9e\u4f8b\u88ab\u521b\u5efa\u65f6\uff0c\n__init__() \u4ec5\u4ec5\u53ea\u662f\u521b\u5efa\u4e00\u4e2a\u5b57\u8282\u6570\u636e\u7684\u5185\u5b58\u89c6\u56fe\uff0c\u6ca1\u6709\u505a\u5176\u4ed6\u4efb\u4f55\u4e8b\u3002\n\u7279\u522b\u7684\uff0c\u8fd9\u65f6\u5019\u5e76\u6ca1\u6709\u4efb\u4f55\u7684\u89e3\u5305\u6216\u8005\u5176\u4ed6\u4e0e\u7ed3\u6784\u76f8\u5173\u7684\u64cd\u4f5c\u53d1\u751f\u3002\n\u8fd9\u6837\u505a\u7684\u4e00\u4e2a\u52a8\u673a\u662f\u4f60\u53ef\u80fd\u4ec5\u4ec5\u53ea\u5bf9\u4e00\u4e2a\u5b57\u8282\u8bb0\u5f55\u7684\u67d0\u4e00\u5c0f\u90e8\u5206\u611f\u5174\u8da3\u3002\u6211\u4eec\u53ea\u9700\u8981\u89e3\u5305\u4f60\u9700\u8981\u8bbf\u95ee\u7684\u90e8\u5206\uff0c\u800c\u4e0d\u662f\u6574\u4e2a\u6587\u4ef6\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u5b9e\u73b0\u61d2\u89e3\u5305\u548c\u6253\u5305\uff0c\u9700\u8981\u4f7f\u7528 StructField \u63cf\u8ff0\u5668\u7c7b\u3002\n\u7528\u6237\u5728 _fields_ \u4e2d\u5217\u51fa\u6765\u7684\u6bcf\u4e2a\u5c5e\u6027\u90fd\u4f1a\u88ab\u8f6c\u5316\u6210\u4e00\u4e2a StructField \u63cf\u8ff0\u5668\uff0c\n\u5b83\u5c06\u76f8\u5173\u7ed3\u6784\u683c\u5f0f\u7801\u548c\u504f\u79fb\u503c\u4fdd\u5b58\u5230\u5b58\u50a8\u7f13\u5b58\u4e2d\u3002\u5143\u7c7b StructureMeta \u5728\u591a\u4e2a\u7ed3\u6784\u7c7b\u88ab\u5b9a\u4e49\u65f6\u81ea\u52a8\u521b\u5efa\u4e86\u8fd9\u4e9b\u63cf\u8ff0\u5668\u3002\n\u6211\u4eec\u4f7f\u7528\u5143\u7c7b\u7684\u4e00\u4e2a\u4e3b\u8981\u539f\u56e0\u662f\u5b83\u4f7f\u5f97\u7528\u6237\u975e\u5e38\u65b9\u4fbf\u7684\u901a\u8fc7\u4e00\u4e2a\u9ad8\u5c42\u63cf\u8ff0\u5c31\u80fd\u6307\u5b9a\u7ed3\u6784\u683c\u5f0f\uff0c\u800c\u65e0\u9700\u8003\u8651\u4f4e\u5c42\u7684\u7ec6\u8282\u95ee\u9898\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "StructureMeta \u7684\u4e00\u4e2a\u5f88\u5fae\u5999\u7684\u5730\u65b9\u5c31\u662f\u5b83\u4f1a\u56fa\u5b9a\u5b57\u8282\u6570\u636e\u987a\u5e8f\u3002\n\u4e5f\u5c31\u662f\u8bf4\uff0c\u5982\u679c\u4efb\u610f\u7684\u5c5e\u6027\u6307\u5b9a\u4e86\u4e00\u4e2a\u5b57\u8282\u987a\u5e8f(<\u8868\u793a\u4f4e\u4f4d\u4f18\u5148 \u6216\u8005 >\u8868\u793a\u9ad8\u4f4d\u4f18\u5148)\uff0c\n\u90a3\u540e\u9762\u6240\u6709\u5b57\u6bb5\u7684\u987a\u5e8f\u90fd\u4ee5\u8fd9\u4e2a\u987a\u5e8f\u4e3a\u51c6\u3002\u8fd9\u4e48\u505a\u53ef\u4ee5\u5e2e\u52a9\u907f\u514d\u989d\u5916\u8f93\u5165\uff0c\u4f46\u662f\u5728\u5b9a\u4e49\u7684\u4e2d\u95f4\u6211\u4eec\u4ecd\u7136\u53ef\u80fd\u5207\u6362\u987a\u5e8f\u7684\u3002\n\u6bd4\u5982\uff0c\u4f60\u53ef\u80fd\u6709\u4e00\u4e9b\u6bd4\u8f83\u590d\u6742\u7684\u7ed3\u6784\uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class ShapeFile(Structure):\n    _fields_ = [ ('>i', 'file_code'), # Big endian\n        ('20s', 'unused'),\n        ('i', 'file_length'),\n        ('<i', 'version'), # Little endian\n        ('i', 'shape_type'),\n        ('d', 'min_x'),\n        ('d', 'min_y'),\n        ('d', 'max_x'),\n        ('d', 'max_y'),\n        ('d', 'min_z'),\n        ('d', 'max_z'),\n        ('d', 'min_m'),\n        ('d', 'max_m') ]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e4b\u524d\u6211\u4eec\u63d0\u5230\u8fc7\uff0cmemoryview() \u7684\u4f7f\u7528\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u907f\u514d\u5185\u5b58\u7684\u590d\u5236\u3002\n\u5f53\u7ed3\u6784\u5b58\u5728\u5d4c\u5957\u7684\u65f6\u5019\uff0cmemoryviews \u53ef\u4ee5\u53e0\u52a0\u540c\u4e00\u5185\u5b58\u533a\u57df\u4e0a\u5b9a\u4e49\u7684\u673a\u6784\u7684\u4e0d\u540c\u90e8\u5206\u3002\n\u8fd9\u4e2a\u7279\u6027\u6bd4\u8f83\u5fae\u5999\uff0c\u4f46\u662f\u5b83\u5173\u6ce8\u7684\u662f\u5185\u5b58\u89c6\u56fe\u4e0e\u666e\u901a\u5b57\u8282\u6570\u7ec4\u7684\u5207\u7247\u64cd\u4f5c\u884c\u4e3a\u3002\n\u5982\u679c\u4f60\u5728\u4e00\u4e2a\u5b57\u8282\u5b57\u7b26\u4e32\u6216\u5b57\u8282\u6570\u7ec4\u4e0a\u6267\u884c\u5207\u7247\u64cd\u4f5c\uff0c\u4f60\u901a\u5e38\u4f1a\u5f97\u5230\u4e00\u4e2a\u6570\u636e\u7684\u62f7\u8d1d\u3002\n\u800c\u5185\u5b58\u89c6\u56fe\u5207\u7247\u4e0d\u662f\u8fd9\u6837\u7684\uff0c\u5b83\u4ec5\u4ec5\u662f\u5728\u5df2\u5b58\u5728\u7684\u5185\u5b58\u4e0a\u9762\u53e0\u52a0\u800c\u5df2\u3002\u56e0\u6b64\uff0c\u8fd9\u79cd\u65b9\u5f0f\u66f4\u52a0\u9ad8\u6548\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd8\u6709\u5f88\u591a\u76f8\u5173\u7684\u7ae0\u8282\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u6269\u5c55\u8fd9\u91cc\u8ba8\u8bba\u7684\u65b9\u6848\u3002\n\u53c2\u80038.13\u5c0f\u8282\u4f7f\u7528\u63cf\u8ff0\u5668\u6784\u5efa\u4e00\u4e2a\u7c7b\u578b\u7cfb\u7edf\u3002\n8.10\u5c0f\u8282\u6709\u66f4\u591a\u5173\u4e8e\u5ef6\u8fdf\u8ba1\u7b97\u5c5e\u6027\u503c\u7684\u8ba8\u8bba\uff0c\u5e76\u4e14\u8ddfNestedStruct\u63cf\u8ff0\u5668\u7684\u5b9e\u73b0\u4e5f\u6709\u5173\u3002\n9.19\u5c0f\u8282\u6709\u4e00\u4e2a\u4f7f\u7528\u5143\u7c7b\u6765\u521d\u59cb\u5316\u7c7b\u6210\u5458\u7684\u4f8b\u5b50\uff0c\u548c StructureMeta \u7c7b\u975e\u5e38\u76f8\u4f3c\u3002\nPython\u7684 ctypes \u6e90\u7801\u540c\u6837\u4e5f\u5f88\u6709\u8da3\uff0c\u5b83\u63d0\u4f9b\u4e86\u5bf9\u5b9a\u4e49\u6570\u636e\u7ed3\u6784\u3001\u6570\u636e\u7ed3\u6784\u5d4c\u5957\u8fd9\u4e9b\u76f8\u4f3c\u529f\u80fd\u7684\u652f\u6301\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}